package com.systop.scos.webapp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;

import com.systop.common.modules.security.user.UserUtil;
import com.systop.common.modules.security.user.model.User;
import com.systop.core.dao.hibernate.BaseHibernateDao;
import com.systop.core.dao.support.Page;
import com.systop.core.util.PageUtil;
import com.systop.core.webapp.struts2.action.BaseAction;
import com.systop.scos.diary.diaryType.model.DiaryType;
import com.systop.scos.diary.model.Diary;
import com.systop.scos.mail.MailConstants;
import com.systop.scos.news.model.News;
import com.systop.scos.news.model.NewsType;
import com.systop.scos.news.service.NewsManager;

/**
 * 后台首页显示各个功能模块列表信息
 * 
 * @author ShangHua
 */
@SuppressWarnings({ "serial", "unchecked", "rawtypes" })
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class MainAction extends BaseAction {

	// 新闻管理Manager
	@Autowired
	private NewsManager newsManager;

	// 数据集合
	private Map<String, List> data = null;

	// 查询语句
	private String hql = null;

	// 某新闻类型下的新闻列表集合
	private List<News> newsByType = new ArrayList<News>();

	// 新闻类型列表集合
	private List<NewsType> nTypes = new ArrayList<NewsType>();

	// 各新闻类型下的新闻集合的集合
	private List newsList = new ArrayList();

	// 日志列表集合
	private List<Diary> diaryByType = new ArrayList<Diary>();

	// 日志类型列表集合
	private List<DiaryType> dTypes = new ArrayList<DiaryType>();

	// 各日志类型下的日志集合的集合
	private List diarysList = new ArrayList();

	/**
	 * 
	 * 查询各个表的数据，提供给首页显示使用(List)
	 * 
	 * @return index.jsp
	 */
	public String index() {
		User user = UserUtil.getPrincipal(getRequest());
		Assert.notNull(user);
		data = new HashMap<String, List>();
		queryNewsTypes();// 新闻类型集合
		queryNews();// 新闻集合
		queryNewsList();// 各新闻类型下的新闻集合的集合
		queryNotices();// 公告集合
		queryDiaryTypes();// 日志类型集合
		queryDiarys();// 日志集合
		queryDiarysList();// 各日志类型下的日志集合的集合
		queryMailBoxs();// 内部邮件集合(当前登录用户的邮件集合)
		return "index";
	}
	
	public String footIndex() {
		queryOnline();
		return "foot";
	}
	/**
	 * 当前在线人数
	 */
	private void queryOnline() {
		hql = "from User u where u.online = 1";
		List userList = getDao().query(hql);
		getRequest().setAttribute("onlineCount", userList.size());
	}
	
	/**
	 * 新闻类型集合
	 */
	private void queryNewsTypes() {
		hql = "from NewsType";
		nTypes = getDao().query(hql);// 新闻类型列表集合
		data.put(MainConstants.NEWS_TYPES, nTypes);
	}
	
	/**
	 * 新闻集合(前7条)
	 */
	private void queryNews() {
		hql = "from News n order by n.createTime desc";
		Page page = PageUtil.getPage(1, 7);
		data.put(MainConstants.NEWS, getDao().pagedQuery(page, hql).getData());
	}

	/**
	 * 各新闻类型下的新闻(前7条)集合的集合
	 */
	private void queryNewsList() {
		hql = "from News n where n.newsType.name=? order by n.createTime desc";
		for (int i = 0; i < nTypes.size(); i++) {
			String type = nTypes.get(i).getName();//类型名
			Page page = PageUtil.getPage(1, 7);
			newsByType = getDao().pagedQuery(page, hql, type).getData();// 某新闻类型下的新闻列表集合
			newsList.add(newsByType);
		}
		// 新闻类型的个数，页面显示用
		getRequest().setAttribute("newsTypeCount", nTypes.size());
	}
	
	/**
	 * 公告集合(前7条)
	 */
	private void queryNotices() {
		hql = "from Notice n order by n.createTime desc";
		Page page = PageUtil.getPage(1, 7);
		data.put(MainConstants.NOTICES, getDao().pagedQuery(page, hql).getData());
	}
	
	/**
	 * 日志类型集合
	 */
	private void queryDiaryTypes() {
		hql = "from DiaryType";
		dTypes = getDao().query(hql);// 日志类型列表集合
		data.put(MainConstants.DIARY_TYPES, dTypes);
	}

	/**
	 * 日志集合(前7条)
	 */
	private void queryDiarys() {
		hql = "from Diary d where d.user.id=? order by d.createDate desc";
		User user = UserUtil.getPrincipal(getRequest());
		Page page = PageUtil.getPage(1, 7);
		data.put(MainConstants.DIARYS,
				getDao().pagedQuery(page, hql, user.getId()).getData());
	}

	/**
	 * 各日志类型下的日志(前7条)集合的集合
	 */
	private void queryDiarysList() {
		hql = "from Diary d where d.diaryType.id=? and d.user.id=? order by d.createDate desc";
		User user = UserUtil.getPrincipal(getRequest());
		for (int j = 0; j < dTypes.size(); j++) {
			int type = dTypes.get(j).getId();
			Page page = PageUtil.getPage(1, 7);
			diaryByType = getDao().pagedQuery(page, hql, type, user.getId()).getData();// 日志列表集合
			diarysList.add(diaryByType);
		}
		// 日志类型的个数，页面显示用
		getRequest().setAttribute("diaryTypeCount", dTypes.size());
	}

	/**
	 * 内部邮件集合(当前登录用户的接收邮件集合,未读前7条)
	 */
	private void queryMailBoxs() {
		hql = "from MailBox mb where mb.user.id=? and mb.type=? and mb.isRead=? and mb.isDel=? order by mb.sendTime desc";
		User user = UserUtil.getPrincipal(getRequest());
		Page page = PageUtil.getPage(1, 7);
		data.put(MainConstants.MAIL_BOXS, 
				getDao().pagedQuery(page, hql, user.getId(), MailConstants.TYPE_RECEVICE, MailConstants.N, MailConstants.N).getData());
	}

	private BaseHibernateDao getDao() {
		// TODO Auto-generated method stub
		return newsManager.getDao();
	}

	public List<NewsType> getnTypes() {
		return nTypes;
	}

	public void setnTypes(List<NewsType> nTypes) {
		this.nTypes = nTypes;
	}

	public List getNewsList() {
		return newsList;
	}

	public void setNewsList(List newsList) {
		this.newsList = newsList;
	}

	public List<DiaryType> getdTypes() {
		return dTypes;
	}

	public void setdTypes(List<DiaryType> dTypes) {
		this.dTypes = dTypes;
	}

	public List getDiarysList() {
		return diarysList;
	}

	public void setDiarysList(List diarysList) {
		this.diarysList = diarysList;
	}

	public List<News> getNewsByType() {
		return newsByType;
	}

	public void setNewsByType(List<News> newsByType) {
		this.newsByType = newsByType;
	}

	public List<Diary> getDiaryByType() {
		return diaryByType;
	}

	public void setDiaryByType(List<Diary> diaryByType) {
		this.diaryByType = diaryByType;
	}

	public Map<String, List> getData() {
		return data;
	}

	public void setData(Map<String, List> data) {
		this.data = data;
	}

}
